package DataBase;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class DataModify extends JInternalFrame {
	Connection conn;
	DatabaseTest main;
	
	private JButton btnModify;
	private JLabel status;
	private JTextField ea;
	private JLabel label_1;
	private JLabel label_2;
	private JTextField code;
	private JTextField serial;
	private JLabel lblSerial;
	private JButton button;
	private JLabel status2;
	private JButton btnDelete;

	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					DataModify frame = new DataModify();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public DataModify(Connection conn, DatabaseTest main) {
		super("수정", true, true, true, false);
		this.conn = conn;
		this.main = main;
		
		setBounds(0, 250, 332, 256);
		getContentPane().setLayout(null);
		getContentPane().add(getBtnModify());
		getContentPane().add(getStatus());
		getContentPane().add(getEa());
		getContentPane().add(getLabel_1());
		getContentPane().add(getLabel_2());
		getContentPane().add(getCode());
		getContentPane().add(getSerial());
		getContentPane().add(getLblSerial());
		getContentPane().add(getButton());
		getContentPane().add(getStatus2());
		getContentPane().add(getBtnDelete());
	}

	private JButton getBtnModify() {
		if (btnModify == null) {
			btnModify = new JButton("수  정");
			btnModify.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					int s =  Integer.parseInt(serial.getText());
					String c = code.getText();
					int e = Integer.parseInt(ea.getText());
					String sql = "update ptbl set code=?, ea=? where serial=?";
					
					try{
						PreparedStatement pstmt =
								conn.prepareStatement(sql);
						pstmt.setString(1, c);
						pstmt.setInt(2, e);
						pstmt.setInt(3, s);
						status.setText("Serial [ "+s+" ] 의 code : "+c+",");
						status2.setText("                         수량 : "+e+"  이 수정이 되었습니다.");
						serial.setText("");
						code.setText("");
						ea.setText("");
						code.requestFocus();
						btnModify.setVisible(false);
						
						pstmt.executeUpdate();
						pstmt.close();
					}catch (Exception ex){
						ex.printStackTrace();
					}
				}
			});
			btnModify.setFont(new Font("나눔바른고딕", Font.BOLD, 15));
			btnModify.setBounds(104, 139, 70, 23);
		}
		return btnModify;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("검색할 코드를 입력하세요.");
			status.setBounds(12, 180, 288, 15);
		}
		return status;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setColumns(10);
			ea.setBounds(104, 102, 116, 21);
		}
		return ea;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("Ea");
			label_1.setFont(new Font("Verdana", Font.BOLD, 20));
			label_1.setBounds(29, 95, 63, 34);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("Code");
			label_2.setFont(new Font("Verdana", Font.BOLD, 20));
			label_2.setBounds(29, 51, 63, 34);
		}
		return label_2;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setColumns(10);
			code.setBounds(104, 61, 116, 21);
		}
		return code;
	}
	private JTextField getSerial() {
		if (serial == null) {
			serial = new JTextField();
			serial.setColumns(10);
			serial.setBounds(104, 20, 116, 21);
		}
		return serial;
	}
	private JLabel getLblSerial() {
		if (lblSerial == null) {
			lblSerial = new JLabel("Serial");
			lblSerial.setFont(new Font("Verdana", Font.BOLD, 20));
			lblSerial.setBounds(29, 13, 76, 34);
		}
		return lblSerial;
	}
	private JButton getButton() {
		if (button == null) {
			button = new JButton("조회");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					if(!(serial.getText().trim().equals(""))){
						try{
							int s = Integer.parseInt(serial.getText());
							String sql = "select * from ptbl where serial = ?";
							PreparedStatement pstmt =
									conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							ResultSet rs = pstmt.executeQuery();
							if(rs.next()){
								code.setText(rs.getString("code"));
								System.out.println(rs.getString("code"));
								ea.setText(""+rs.getInt("ea"));
								status.setText("Serial [ "+s+" ] 의"+"'"
												+code.getText()+"'가 검색되었습니다.");
								status2.setText("");

							}else{
								status.setText("Serial [ "+s +" ] 의 데이터가 검색되지 않았습니다.");
								code.setText("");
								ea.setText("");
								serial.setText("");
							}
							rs.close();
							pstmt.close();
						}catch (Exception ex){
							ex.printStackTrace();
							JOptionPane.showMessageDialog(null, "Serial번호는 숫자를 입력하셔야 해요.");
						}
					}else{
						JOptionPane.showMessageDialog(null, "Serial번호를 입력해주세요.");
						code.setText("");
						ea.setText("");
					}
					serial.requestFocus();
				}
			});
			button.setFont(new Font("나눔바른고딕", Font.BOLD, 15));
			button.setBounds(231, 19, 61, 23);
		}
		return button;
	}
	private JLabel getStatus2() {
		if (status2 == null) {
			status2 = new JLabel("");
			status2.setBounds(12, 205, 288, 15);
		}
		return status2;
	}
	
	public void setData(String s, String c, String e){
		serial.setText(s);
		code.setText(c);
		ea.setText(e);
	}
	private JButton getBtnDelete() {
		if (btnDelete == null) {
			btnDelete = new JButton("삭  제");
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {					
						try{
							int s = Integer.parseInt(serial.getText());
							String sql = "delete from ptbl where serial = ?";
							PreparedStatement pstmt =
									conn.prepareStatement(sql);
							pstmt.setInt(1, s);
							pstmt.executeUpdate();
							
							pstmt.close();
							status.setText("Serial [ "+ s +" ] 의 데이터가 삭제되었습니다.");
							serial.setText("");
							code.setText("");
							ea.setText("");
							serial.requestFocus();

						}catch (Exception ex){
							ex.printStackTrace();
						}
				}
			});
			btnDelete.setFont(new Font("나눔바른고딕", Font.BOLD, 15));
			btnDelete.setBounds(186, 138, 70, 23);
		}
		return btnDelete;
	}
}
